import { defineStore } from 'pinia'
import cache from '@/utils/cache'
export default defineStore('user', {
  state: () => ({
    userinfo:null,
    group:null,
    roles:[],
    permissions:[],
    token:'',
    themes:'',
  }),
  getters: {
    user: (state) => state.userinfo, // 获取用户信息
    pageTheme: (state) => state.themes||cache.getTheme, // 获取用户组
  },
  actions: {
    setToken(token:string){
        this.$state.token = token
        cache.setToken(token)
    },
    setTheme(text:string){ // 设置主题
        this.$state.themes = text
        cache.setTheme(text)
    },
    reset(){
        /* this.$patch 可以同时更改多个state状态属性 */
        this.$reset()
    },
    setUserInfo(userinfo:any){
        const {group,role,permissions} = userinfo
        this.group = group
        this.roles = role
        this.userinfo = userinfo
        this.permissions = permissions
    },
  }
})
